/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ortustve.onlinescrapper.themoviedb;

import java.io.Serializable;
import java.util.List;
import sagex.api.MediaFileAPI;

/**
 *
 * @author jphipps
 */
public class ImageItem extends ortustve.ortusvars implements Serializable {

    private String type;
    private String url = "";
    private String size;

    public ImageItem() {
    }

    public void SetType(String type) {
        this.type = type.replace("backdrop","Backgrounds");
        this.type = this.type.replace("poster","Posters");
    }
    public void SetSize(String size) {
	    this.size=size;
    }
    public void SetUrl(String url) {
	    this.url = url;
    }

    public ImageItem(String type, String size, String url) {
        this.type = type.replace("backdrop","Backgrounds");
        this.type = this.type.replace("poster","Posters");
        this.size = size;
        this.url = url;
        ortustve.api.DebugLog(TRACE2, " ImageItem: " + type + " url: " + url);
    }

    public boolean IsPoster() {
        if( type.equals("Posters"))
            return true;
        else
            return false;
    }

    public boolean IsBackgrounds() {
        if( type.equals("Backgrounds"))
            return true;
        else
            return false;
    }

    public boolean IsBanner() {
        if ( type.equals("Banners"))
            return true;
        else
            return false;
    }
    
    public String GetUrl() {
        return url;
    }

    public void WriteImageDB(Object mediafile) {
//       String SQL ="UPDATE sage.fanart SET type = '"+ type +"', url='" + url + "' WHERE mediaid = " + MediaFileAPI.GetMediaFileID(mediafile);
//        int success = ortus.api.executeSQL(SQL);
//        if(success < 1){
          String  SQL =  "INSERT INTO sage.fanart (mediaid, type, url) VALUES(" + MediaFileAPI.GetMediaFileID(mediafile) + ", '" + type + "','" + url + "')";
          ortustve.api.executeSQL(SQL);
//        }
    }

    public void WriteFileImageDB(Object mediafile) {
	  String fafilename = url.substring(url.lastIndexOf(java.io.File.separator) + 1);
	  List<Object> result = ortustve.api.executeSQLQuery("select mediaid from sage.fanart where mediaid = " + MediaFileAPI.GetMediaFileID(mediafile) + " and url like '%/" + fafilename + "'");
	  if ( result.size() > 0) {
		  String SQL = "update sage.fanart set file='" + url + "' where mediaid = " + MediaFileAPI.GetMediaFileID(mediafile) + " and url like '%/" + fafilename + "'";
		  ortustve.api.executeSQL(SQL);
	  } else {
		  String  SQL =  "INSERT INTO sage.fanart (mediaid, type, url, file) VALUES(" + MediaFileAPI.GetMediaFileID(mediafile) + ", '" + type + "','" + url + "','" + url+"')";
		  ortustve.api.executeSQL(SQL);
	  }
    }
    
    public void getImage(Object mediafile, String destination) {

        String downloaddir = ortustve.api.GetFanartFolder() + java.io.File.separator + destination + java.io.File.separator + type;
        String filename = url.substring(url.lastIndexOf("/") + 1);
        
//        ortus.api.DebugLog(TRACE2, " getImage: " + filename + " dir: " + downloaddir);

 //       File df = new File(downloaddir);
 //       if ( ! df.exists())
 //           df.mkdirs();

//        downloaddir+= java.io.File.separator + filename;
        	ortustve.onlinescrapper.tools.urldownload.fileDownload(url, downloaddir );

        String SQL ="UPDATE sage.fanart SET file = '"+ destination + java.io.File.separator + type + java.io.File.separator + filename + "' WHERE mediaid = " + MediaFileAPI.GetMediaFileID(mediafile) + " and type = '" + type + "' and url = '" + url + "'";
        int success = ortustve.api.executeSQL(SQL);

        return ;
    }
}
